// mycomponent/countryCode/countryCode.js
const app = getApp();
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    openPopUp: {
      type: Boolean,
      value: false
    },
    optValue: {
      type: String,
      value: ""
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    country_code_arr:[]
  },
  lifetimes: {
    attached() {
      this.getData();
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    close(){
      this.setData({
        openPopUp:false
      })
      this.triggerEvent('close',false)
    },
    onChange(e){
      this.triggerEvent('childSelect',e.detail);
    },
    onClick(e){
      this.setData({
        optValue:e.currentTarget.dataset.code
      })
      this.triggerEvent('childSelect',e.currentTarget.dataset.code);
    },
    async getData() {
      wx.request({
        url: app.data.host+'/addons/yun_shop/static/app/country_code.js',
        success: (res) => {
          if(res.statusCode == 200){
            let data = res.data;
            const startIndex = data.indexOf('[');
            const endIndex = data.lastIndexOf(']') + 1;
            const jsonString = data.slice(startIndex, endIndex);
  
            // 2. 使用 JSON.parse() 解析
            const COUNTRYCODEARR = JSON.parse(jsonString);
            this.setData({
              country_code_arr:COUNTRYCODEARR
            })
          }
        }
      })
    }
  }
})
